package com.example.nick.bodyapplication.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class MotionArea extends View {

    int row = 3, column = 3;
    int itemW = 0, itemH = 0;
    Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);

    public MotionArea(Context context) {
        super(context);
        init();
    }

    public MotionArea(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MotionArea(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paintLine.setColor(Color.WHITE);
        paintLine.setStrokeWidth(5);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        canvas.drawCircle(300, 300, 200, paintLine);
        itemW = getWidth() / column;
        itemH = getHeight() / row;
        canvas.save();
        drawRowLine(canvas);
        drawColumnLine(canvas);
        canvas.restore();
    }

    private void drawRowLine(Canvas canvas) {
        for (int i=1; i<column; i++) {
            float y = (i * itemH);
            Log.i("area", "area itemH:"+itemH+", y:"+y);
            canvas.drawLine(0.0f, y, getWidth(), y, paintLine);
        }
    }

    private void drawColumnLine(Canvas canvas) {
        for (int i=1; i<row; i++) {
            float x = (i * itemW);
            Log.i("area", "area itemW:"+itemW+", x:"+x);
            canvas.drawLine(x, 0, x, getHeight(), paintLine);
        }
    }
}
